﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Common.DataAnnotations
{
    /// <summary>
    /// 短信验证码格式验证器
    /// </summary>
    public class SMSAuthCodeAttribute : ValidationAttribute
    {
        /// <summary>
        /// 短信验证码格式验证器
        /// </summary>
        public SMSAuthCodeAttribute()
        {
            this.Length = 6;
        }
        /// <summary>
        /// 短信验证码格式验证器
        /// </summary>
        /// <param name="length">验证码位数</param>
        public SMSAuthCodeAttribute(int length)
        {
            this.Length = length;
        }
        private readonly int Length;

        /// <inheritdoc/>
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }
            var temp = value.ToString();
            if (temp.Length != Length)
            {
                if (string.IsNullOrEmpty(ErrorMessage))
                    ErrorMessage = "{0}无效！";
                return false;
            }
            return true;
        }
    }
}
